
/**
 * name:        地区三级联动
 * author:      wuwq & lyj
 * Time:        18/01/26
 * Copyright:   chinadangmai.com
 */

function setArea(token,URL){
    $.ajax({
        type:'post',
        url:new_url+'shop/home/areaInfo',
        dataType:'json',
        data:{token:token,parentId:0},
        success:function(e){
            for(var i=0;i<e.data.length;i++){
                var t='<option value="'+e.data[i].area_id+'">'+e.data[i].area_name+'</option>';
                $("#province").append(t);
            }
            
        }
    });
    $("#province").on('change',function(){
        var parentId = ($(this).val());
        $.ajax({
            type:'post',
            url:new_url+'shop/home/areaInfo',
            dataType:'json',
            data:{token:token,parentId:parentId},
            success:function(e){
                $("#city option, #area option").remove();
                $("#city, #area").append('<option>请选择</option></select>');
                for(var i=0;i<e.data.length;i++){
                    var t='<option value="'+e.data[i].area_id+'">'+e.data[i].area_name+'</option>';
                    $("#city").append(t);
                }
            }
        });
    });
    $("#city").on('change',function(){
        var parentId = ($(this).val());
        $.ajax({
            type:'post',
            url:new_url+'shop/home/areaInfo',
            dataType:'json',
            data:{token:token,parentId:parentId},
            success:function(e){
                $("#area option").remove();
                $("#area").append('<option>请选择</option></select>');
                for(var i=0;i<e.data.length;i++){
                    var t='<option value="'+e.data[i].area_id+'">'+e.data[i].area_name+'</option>';
                    $("#area").append(t);
                }
            }
        });
    });
}

// 设为默认地址
function isDefault(token,URL){
    $(".address_set_default").click(function(){
        var isDefault = $(this).attr('data-isDefault');
        var addressId = $(this).attr('data-addres-id');
        if(isDefault!=1){   // 点击   常规地址设为默认地址  
            isDefault=1;
            $.ajax({
                type:'post',
                url:new_url+'shop/member/updateShippingAddress',
                dataType:'json',
                data:{token:token,addressId:addressId,isDefault:isDefault}, 
                success:function(e){
                    window.location.reload();
                }
            });
        }else{      // 点击   默认地址改成常规地址
            isDefault=0;
            $.ajax({
                type:'post',
                url:new_url+'shop/member/updateShippingAddress',
                dataType:'json',
                data:{token:token,addressId:addressId,isDefault:isDefault}, 
                success:function(e){
                    window.location.reload();
                }
            });
        }
    })
}

// 删除收货地址
function deleteAddress(token,URL){
    $('.delete_address').click(function(){
        var addressId = $(this).parents('li').attr('data-addres-id');
        $.ajax({
            type:'post',
            url:new_url+'shop/member/deleteShippingAddress',
            dataType:'json',
            data:{token:token,addressId:addressId}, 
            success:function(e){
                window.location.reload();
            }
        })
    });
}

// 编辑地址弹窗
function editAddressPop(addressId,addressee,areaId,cityId,provinceId,areaInfo,address,phone,isDefault,token,URL){
    var t = ''
    t+='<form class="site_bg" action="" method="post">'
    t+='<div class="popover_m"></div>'
    t+='<div class="site_bg">'
    t+='<dl>'
    t+='<dt>修改收货地址</dt>'
    t+='<dd>'
    t+='<em>联系人</em><input type="text" id="name" value="' +addressee+ '">'
    t+='</dd>'
    t+='<dd>'
    t+='<em>联系电话</em><input type="text" id="phone" value="' +phone+' ">'
    t+='</dd>'
    t+='<dd>'
    t+='<em>所在区域</em>'
    t+='<select name="" id="province"></select>'
    t+='<select name="" id="city"></select>'
    t+='<select name="" id="area"></select>'
    t+='</dd>'
    t+='<dd style="margin-bottom:15px;">'
    t+='<em style="line-height:50px;">详细地址</em>'
    t+='<textarea name=""  id="addressTxt" cols="30" rows="10">'+address+'</textarea>'
    t+='</dd>'
    t+='<dd style="margin-bottom:15px;">'
    t+='<input type="checkbox" checked="checked"  name="" id="checked_true">设为默认'
    t+='</dd>'
    t+='<dd style="height:50px;"><a href="javascript:;" class="addAddress_btn" id="submit">提交</a></dd>'
    t+='</dl>'
    t+='<span class="gb_01" style="display:block;width:18px;height:18px;background:url(../../img/base/icon_06.png) 0 0 no-repeat;float:right;margin:40px 30px 0 0;cursor: pointer;"></span>'
    t+='</div>'
    t+='</form>'
    $('body').append(t);
    $('.popover_m, .gb_01').click(function(){
        $('.popover_m, .site_bg').remove(); 
    });
    var areaInfoTwo = areaInfo.split(' ');
    for(i in areaInfoTwo){
        province=areaInfoTwo[0];
        city=areaInfoTwo[1];
        if(areaInfoTwo[2] == '' || areaInfoTwo[2] == null){
            area='请选择'
        }else{
            area=areaInfoTwo[2];
        }
        
    }
    $("#province").append('<option value="'+provinceId+'">'+province+'</option>');
    $("#city").append('<option value="'+cityId+'">'+city+'</option>');
    $("#area").append('<option value="'+areaId+'">'+area+'</option>');
    $.ajax({
        type:'post',
        url:new_url+'shop/home/areaInfo',
        dataType:'json',
        data:{token:token,parentId:0},
        success:function(e){
            for(var i=0;i<e.data.length;i++){
                var t='<option value="'+e.data[i].area_id+'">'+e.data[i].area_name+'</option>';
                $("#province").append(t);
            }
            
        }
    });
    $.ajax({
        type:'post',
        url:new_url+'shop/home/areaInfo',
        dataType:'json',
        data:{token:token,parentId:provinceId},
        success:function(e){
            for(var i=0;i<e.data.length;i++){
                var t='<option value="'+e.data[i].area_id+'">'+e.data[i].area_name+'</option>';
                $("#city").append(t);
            }
            
        }
    });
    $.ajax({
        type:'post',
        url:new_url+'shop/home/areaInfo',
        dataType:'json',
        data:{token:token,parentId:cityId},
        success:function(e){
            for(var i=0;i<e.data.length;i++){
                var t='<option value="'+e.data[i].area_id+'">'+e.data[i].area_name+'</option>';
                $("#area").append(t);
            }
            
        }
    });
    // 确认修改
    $("#submit").click(function(){
        var addressee=$("#name").val();
        var phone =$("#phone").val();
        var address =$("#addressTxt").val();
        var provinceId=$("#province").val();
        var cityId=$("#city").val();
        var areaId=$("#area").val();
        var areaInfo=$("#province").find("option:selected").text()+' '+$("#city").find("option:selected").text()+' '+$("#area").find("option:selected").text();
        areaValidate(addressee,phone,provinceId,cityId,areaId,address,isDefault)
        if($('#checked_true').attr('checked')){
            isDefault=1;
        }else{
            isDefault=0;
        }
        var data = {
            token:token,
            addressee:addressee,
            phone:phone,
            address:address,
            addressId:addressId,
            isDefault:isDefault,
            provinceId:provinceId,
            cityId:cityId,
            areaId:areaId,
            areaInfo:areaInfo
        }
        $.ajax({
            type:'post',
            url:new_url+'shop/member/updateShippingAddress',
            dataType:'json',
            data:data, 
            success:function(e){ 
                if(e.code=='0'){
                    tc('地址修改成功');
                    setTimeout(function(){
                        window.location.reload();
                    },1000);
                }
                if(e.code=='7002'){
                    tc('默认地址无法修改成普通地址');
                }
            }
        });
    });
    /* ----------------- 地区三级联动 ----------------- */
    setArea(token,URL);
}


// 新增地址弹窗
function addAddressPop(token,URL){
    var t = ''
    t+='<form class="site_bg" action="" method="post">'
    t+='<div class="popover_m"></div>'
    t+='<div class="site_bg">'
    t+='<dl>'
    t+='<dt>新增收货地址</dt>'
    t+='<dd>'
    t+='<em>联系人</em><input type="text" id="name" value="">'
    t+='</dd>'
    t+='<dd>'
    t+='<em>联系电话</em><input type="text" id="phone" value="">'
    t+='</dd>'
    t+='<dd>'
    t+='<em>所在区域</em>'
    t+='<select name="" id="province"><option>请选择</option></select>'
    t+='<select name="" id="city"><option>请选择</option></select>'
    t+='<select name="" id="area"><option>请选择</option></select>'
    t+='</dd>'
    t+='<dd style="margin-bottom:15px;">'
    t+='<em style="line-height:50px;">详细地址</em>'
    t+='<textarea name=""  id="addressTxt" cols="30" rows="10"></textarea>'
    t+='</dd>'
    t+='<dd style="margin-bottom:15px;">'
    t+='<input type="checkbox" checked="checked"  name="" id="checked_true">设为默认'
    t+='</dd>'
    t+='<dd style="height:50px;"><a href="javascript:;" class="addAddress_btn" id="submit">提交</a></dd>'
    t+='</dl>'
    t+='<span class="gb_01" style="display:block;width:18px;height:18px;background:url(../../img/base/icon_06.png) 0 0 no-repeat;float:right;margin:40px 30px 0 0;cursor: pointer;"></span>'
    t+='</div>'
    t+='</form>'
    $('body').append(t);
    $('.popover_m, .gb_01').click(function(){
        $('.popover_m, .site_bg').remove(); 
    });
    var num=0
    $(".address_wrap ul li").each(function(){
        num = num +1;
    }); 
    // 确认修改
    $("#submit").click(function(){ 
        if(num>8){
            tc('只能添加9个地址！')
        }else{
            var addressee=$("#name").val();
            var phone =$("#phone").val();
            var address =$("#addressTxt").val();
            var provinceId=$("#province").val();
            var cityId=$("#city").val();
            var areaId=$("#area").val();
            var areaInfo=$("#province").find("option:selected").text()+' '+$("#city").find("option:selected").text()+' '+$("#area").find("option:selected").text(); 
            if(addressee == '' || addressee==null){
                tc("请填写收货人姓名");
                return false;
            }
            if(!(/^1(3|4|5|7|8)\d{9}$/.test(phone))){
                tc("请输入正确的手机号")
                return false;    
            }
            if(provinceId == '请选择' || addressee==null){
                tc("请选择所在省份");
                return false;
            }
            if(cityId == '请选择' || addressee==null){
                tc("请选择所在城市");
                return false;
            }
            // if(areaId == '请选择' || addressee==null){
            //     tc("请选择所在区域");
            //     return false;
            // }
            if(address == '' || addressee==null){
                tc("请填写收货人的详细地址");
                return false;
            }
            if($('#checked_true').attr('checked')){
                isDefault=1;
            }else{
                isDefault=0;
            }
            var data = {
                token:token,
                addressee:addressee,
                phone:phone,
                address:address,
                isDefault:isDefault,
                provinceId:provinceId,
                cityId:cityId,
                areaId:areaId,
                areaInfo:areaInfo
            }
            $.ajax({
                type:'post',
                url:new_url+'shop/member/addShippingAddress',
                dataType:'json',
                data:data, 
                success:function(e){ 
                    if(e.code='0'){
                        tc('地址添加成功');
                        setTimeout(function(){
                            window.location.reload();
                        },1000);
                    }
                    else{
                        tc(e.message)  
                    }
                }
            });
        }
        
    });
    /* ----------------- 地区三级联动 ----------------- */
    setArea(token,URL);
}

function areaValidate(addressee,phone,provinceId,cityId,areaId,address,isDefault){
    if(addressee == '' || addressee==null){
        tc("请填写收货人姓名");
        return false;
    }
    // if(!(/^1(3|4|5|7|8)\d{9}$/.test(phone))){
    //     tc("请输入正确的手机号")   
    // }
    if(provinceId == '请选择' || addressee==null){
        tc("请选择所在省份");
        return false;
    }
    if(cityId == '请选择' || addressee==null){
        tc("请选择所在城市");
        return false;
    }
    // if(areaId == '请选择' || addressee==null){
    //     tc("请选择所在区域");
    //     return false;
    // }
    if(address == '' || addressee==null){
        tc("请填写收货人的详细地址");
        return false;
    }
    
}